package service.hansot;

import hansot.model.dao.MenuDAO;
import hansot.to.CategoryTO;
import hansot.to.CommentTO;
import hansot.to.GradeTO;
import hansot.to.MenuGradeTO;
import hansot.to.MenuTO;
import hansot.util.Utilities;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;


public class MenuService {
	private MenuDAO dao;

	public MenuService(MenuDAO dao) {
		this.dao = dao;
	}

	
	public MenuTO getMenuById(String id) throws SQLException {
		return dao.selectMenuById(id);
	}
	
	public ArrayList<CommentTO> getCommentByMenuId(String id) throws SQLException{
		return dao.selectCommentByMenuId(id);
	}
	
	public ArrayList<MenuTO> selectMenuByCategory(String categoryId) throws SQLException{
		return (ArrayList<MenuTO>)dao.selectMenuByCategory(categoryId);
	}
	public ArrayList<CategoryTO> selectCategory()throws SQLException{
		return (ArrayList<CategoryTO>)dao.selectCategory();
	}
	
	public void insertComment(CommentTO comm) throws SQLException{
		comm.setCommentDate(Utilities.getNow());
		dao.insertComment(comm);
	}

	
	public void addMenuGrade(GradeTO gto) throws SQLException{
		MenuGradeTO mgto = new MenuGradeTO();
		mgto.setMenuId(gto.getMenuId());
		mgto.setTotalGrade(gto.getGradeNumber());

		if(dao.selectMenuGradeByMenuId(gto) == null){
			dao.insertMenuTotalGrade(gto.getMenuId());
		}
		dao.updateMenuTotalGrade(mgto);
		System.out.println(gto);
		dao.insertGrade(gto);
		
		int count;
		int total;
		double result=0;
		try{
			count = dao.countGradeByMenuId(gto.getMenuId());
			total = dao.getMenuTotalGrade(gto.getMenuId());
			System.out.println(count +" - "+total);
			result = (double)total/(double)count;
			System.out.println(result);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("익셉션");
		}
		MenuTO mto = new MenuTO();
		mto.setMenuId(gto.getMenuId());
		mto.setGrade(result);
		
		dao.updateMenuGradeByMenuId(mto);
	}
}
